#!/bin/sh

# update_kernel_extensions: Manage the replacement of old kernel extensions.
#                           If there are updates, kill kextd and destroy the
#                           caches.

# Get common scripts.
. /etc/um-common.sh

OSVersion="$(/usr/bin/uname -r | awk -F '.' '{ print $1 }')"

if [ "${OSVersion}" == "9" ]; then # Leopard
	KEXT_CACHE="/System/Library/Extensions.mkext"
elif [ "${OSVersion}" == "10" ]; then # Snow Leopard
	KEXT_CACHE="/System/Library/Caches/com.apple.kext.caches"
fi

KEXT_FOLDER="/System/Library/Extensions"
KEXTD_LAUNCHD="com.apple.kextd"
SYSTEM_LAUNCHD_FOLDER="/System/Library/LaunchDaemons"

transcript="${1}"
result="${transcript}.$$"

/usr/bin/grep "${KEXT_FOLDER}" "${transcript}" > "${result}"

if test -n "${result}"; then #result is non-empty
	# Disable kextd to prevent it from recreating kernel extension caches, which
	# will be re-created at startup.
	if test -n "$(/bin/launchctl list | /usr/bin/grep ${KEXTD_LAUNCHD})"; then
		#kextd is running
		/bin/launchctl unload "${SYSTEM_LAUNCHD_FOLDER}/${KEXTD_LAUNCHD}.plist"
	fi
	
	# Remove kernel extension cache.
	/bin/rm -rf "${KEXT_CACHE}"
fi

rm -f "${result}"
